package com.beamfield.controller.manage.steelplant;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.model.Supplies;
import com.beamfield.service.manage.sys.SuppliesService;
import com.beamfield.service.serviceimpl.manage.steelplant.SteelplantPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author Zephyr
 * @description:
 * @date 2019/7/17
 */
@Controller
@RequestMapping("/cms/steelplant/plan")
public class PlanController extends BaseController {

    @Autowired
    private SteelplantPlanService steelplantPlanService;

    @Autowired
    private SuppliesService suppliesService;


    @RequestMapping("/plan")
    public ModelAndView plan(){
        return new ModelAndView();
    }

    @RequestMapping("/month_plan")
    public ModelAndView monthPlan(){
        return new ModelAndView();
    }

    @RequestMapping("/edit_plan")
    public ModelAndView editPlan(){
        return new ModelAndView();
    }

    @RequestMapping("/month_plan_detail")
    public ModelAndView monthPlanDetail(){
        return new ModelAndView();
    }

    @RequestMapping("/edit_month_plan")
    public ModelAndView editMonthPlan(){
        return new ModelAndView();
    }

    @RequestMapping("/demand_month_plan")
    public ModelAndView demandMonthPlan(){
        return new ModelAndView();
    }

    @RequestMapping("/demand_plan")
    public ModelAndView demandPlan(){
        return new ModelAndView();
    }

    @RequestMapping("/demand_month_plan_detail")
    public ModelAndView demandMonthPlanDetail(){
        return new ModelAndView();
    }

    @RequestMapping("/storage_management")
    public ModelAndView storageManagement(){
        return new ModelAndView();
    }

    @RequestMapping("/edit_rebar_inventory")
    public ModelAndView editRebarInventory(){
        return new ModelAndView();
    }

    @RequestMapping("/rebar_inventory")
    public ModelAndView rebarInventory(){
        return new ModelAndView();
    }

    @RequestMapping("/simple_inventory_detail")
    public ModelAndView simpleInventoryDetail(){
        return new ModelAndView();
    }

    @RequestMapping("/edit_simple_inventory")
    public ModelAndView editSimpleInventory(){
        return new ModelAndView();
    }

    @RequestMapping("/simple_inventory")
    public ModelAndView simpleInventory(){
        return new ModelAndView();
    }

    @RequestMapping("/rebar_inventory_detail")
    public ModelAndView rebarInventoryDetail(){
        return new ModelAndView();
    }

    @RequestMapping("/doSavePlan")
    @ResponseBody
    public ApiResult doSavePlan(HttpServletRequest request, String planJson){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        return steelplantPlanService.savePlan(beamId, userId, planJson);
    }

    @RequestMapping("/editPlan")
    @ResponseBody
    public ApiResult editPlan(Long id, Integer num){
        return steelplantPlanService.editPlan(id, num);
    }

    @RequestMapping("/readPlan")
    @ResponseBody
    public PageResult readPlan(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.readPlan(offset, limit, beamId);
    }

    @RequestMapping("/deletePlan")
    @ResponseBody
    public ApiResult deletePlan(HttpServletRequest request){
        String id = request.getParameter("id");
        return steelplantPlanService.deletePlan(id);
    }

    @RequestMapping("/exportPlan")
    public ApiResult exportPlan(HttpServletRequest request, HttpServletResponse response){
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.exportPlan(beamId, response);
    }


    @RequestMapping("/doSaveMonthPlan")
    @ResponseBody
    public ApiResult doSaveMonthPlan(HttpServletRequest request, String planJson){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        return steelplantPlanService.doSaveMonthPlan(beamId, userId, planJson);
    }

    @RequestMapping("/readMonthPlan")
    @ResponseBody
    public PageResult readMonthPlan(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.readMonthPlan(offset, limit, beamId);
    }

    @RequestMapping("/deleteMonthPlan")
    @ResponseBody
    public ApiResult deleteMonthPlan(HttpServletRequest request){
        String id = request.getParameter("id");
        return steelplantPlanService.deleteMonthPlan(id);
    }

    @RequestMapping("/readMonthPlanDetail")
    @ResponseBody
    public PageResult readMonthPlanDetail(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String monthId = request.getParameter("monthId");
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.readMonthPlanDetail(offset, limit, beamId, Long.parseLong(monthId));
    }

    @RequestMapping("/deleteMonthPlanDetail")
    @ResponseBody
    public ApiResult deleteMonthPlanDetail(HttpServletRequest request){
        String id = request.getParameter("id");
        return steelplantPlanService.deleteMonthPlanDetail(id);
    }

    @RequestMapping("/exportMonthPlan")
    public ApiResult exportMonthPlan(HttpServletRequest request, HttpServletResponse response){
        Long beamId = super.getBeamId(request);
        String monthId = request.getParameter("monthId");
        return steelplantPlanService.exportMonthPlan(beamId, Long.parseLong(monthId), response);
    }


    @RequestMapping("/readDemandPlan")
    @ResponseBody
    public PageResult readDemandPlan(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.readDemandPlan(offset, limit, beamId);
    }

    @ResponseBody
    @RequestMapping("/deleteDemandPlan")
    public ApiResult deleteDemandPlan(HttpServletRequest request){
        String id = request.getParameter("id");
        return steelplantPlanService.deleteDemandPlan(id);
    }


    @RequestMapping("/getUsepartOfDemand")
    @ResponseBody
    public ApiResult getUsepartOfDemand(HttpServletRequest request){
        String suppliesId = request.getParameter("suppliesId");
        return steelplantPlanService.getUsepartOfDemand(suppliesId);
    }

    @RequestMapping("/getUsepartOfDemandMonth")
    @ResponseBody
    public ApiResult getUsepartOfDemandMonth(HttpServletRequest request){
        String suppliesId = request.getParameter("suppliesId");
        String monthId = request.getParameter("monthId");
        return steelplantPlanService.getUsepartOfDemandMonth(monthId, suppliesId);
    }

    @RequestMapping("/editDemandEntryTime")
    @ResponseBody
    public ApiResult editDemandEntryTime(HttpServletRequest request){
        String suppliesId = request.getParameter("supplies_id");
        String date = request.getParameter("date");
        return steelplantPlanService.editDemandEntryTime(suppliesId, date);
    }

    @RequestMapping("/editDemandPlan")
    @ResponseBody
    public ApiResult editDemandPlan(Long id, BigDecimal num){
        return steelplantPlanService.editDemandPlan(id, num);
    }

    @RequestMapping("/exportDemandPlan")
    public ApiResult exportDemandPlan(HttpServletRequest request, HttpServletResponse response){
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.exportDemandPlan(beamId, response);
    }

    @RequestMapping("/readDemandMonthPlan")
    @ResponseBody
    public PageResult readDemandMonthPlan(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.readDemandMonthPlan(offset, limit, beamId);
    }


    @ResponseBody
    @RequestMapping("/deleteDemandMonthPlan")
    public ApiResult deleteDemandMonthPlan(HttpServletRequest request){
        String id = request.getParameter("id");
        return steelplantPlanService.deleteDemandMonthPlan(id);
    }

    @RequestMapping("/readDemandMonthPlanDetail")
    @ResponseBody
    public PageResult readDemandMonthPlanDetail(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String monthId = request.getParameter("monthId");
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.readDemandMonthPlanDetail(offset, limit, beamId, monthId);
    }

    @RequestMapping("/editDemandMonthEntryTime")
    @ResponseBody
    public ApiResult editDemandMonthEntryTime(HttpServletRequest request){
        String suppliesId = request.getParameter("supplies_id");
        String date = request.getParameter("date");
        String monthId = request.getParameter("monthId");
        return steelplantPlanService.editDemandMonthEntryTime(monthId, suppliesId, date);
    }

    @RequestMapping("/editDemandMonthPlanDetail")
    @ResponseBody
    public ApiResult editDemandMonthPlanDetail(Long id, BigDecimal num){
        return steelplantPlanService.editDemandMonthPlanDetail(id, num);
    }

    @RequestMapping("/exportDemandMonthPlan")
    public ApiResult exportDemandMonthPlan(HttpServletRequest request, HttpServletResponse response){
        Long beamId = super.getBeamId(request);
        String monthId = request.getParameter("monthId");
        return steelplantPlanService.exportDemandMonthPlan(beamId, Long.parseLong(monthId), response);
    }

    @RequestMapping(value = "/getSuppliesName", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getSuppliesName(HttpServletRequest request) {
        ApiResult apiResult = new ApiResult();
        long beamId = super.getBeamId(request);
        String projectId = request.getParameter("projectId");
        List<Supplies> suppliesList = suppliesService.getSuppliesDistinctByNameAndMaterialName("020104", beamId, projectId);
        apiResult.setResult(suppliesList);
        return apiResult;
    }

    @RequestMapping("/readRebarInventory")
    @ResponseBody
    public PageResult readRebarInventory(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.readRebarInventory(offset, limit, beamId);
    }

    @RequestMapping("/deleteRebarInventory")
    @ResponseBody
    public ApiResult deleteRebarInventory(HttpServletRequest request){
        String id = request.getParameter("id");
        return steelplantPlanService.deleteRebarInventory(id);
    }

    @RequestMapping("/doSaveRebarInventory")
    @ResponseBody
    public ApiResult doSaveRebarInventory(HttpServletRequest request, String inventoryJson){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        return steelplantPlanService.doSaveRebarInventory(beamId, userId, inventoryJson);
    }

    @RequestMapping("/readRebarInventoryDetail")
    @ResponseBody
    public PageResult readRebarInventoryDetail(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String inventoryId = request.getParameter("inventoryId");
        return steelplantPlanService.readRebarInventoryDetail(offset, limit, inventoryId);
    }

    @RequestMapping("/readSimpleInventory")
    @ResponseBody
    public PageResult readSimpleInventory(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.readSimpleInventory(offset, limit, beamId);
    }

    @RequestMapping("/deleteSimpleInventory")
    @ResponseBody
    public ApiResult deleteSimpleInventory(HttpServletRequest request){
        String id = request.getParameter("id");
        return steelplantPlanService.deleteSimpleInventory(id);
    }

    @RequestMapping("/doSaveSimpleInventory")
    @ResponseBody
    public ApiResult doSaveSimpleInventory(HttpServletRequest request, String inventoryJson){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        return steelplantPlanService.doSaveSimpleInventory(beamId, userId, inventoryJson);
    }

    @RequestMapping("/readSimpleInventoryDetail")
    @ResponseBody
    public PageResult readSimpleInventoryDetail(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String inventoryId = request.getParameter("inventoryId");
        return steelplantPlanService.readSimpleInventoryDetail(offset, limit, inventoryId);
    }

    @RequestMapping("/getUsepart")
    @ResponseBody
    public ApiResult getUsepart(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        return steelplantPlanService.getUsepart(beamId);
    }

    @RequestMapping("/getUsepartRebar")
    @ResponseBody
    public ApiResult getUsepartRebar(HttpServletRequest request){
        String usepartId = request.getParameter("usepartId");
        return steelplantPlanService.getUsepartRebar(usepartId);
    }

    @RequestMapping("/getRebarWeight")
    @ResponseBody
    public ApiResult getRebarWeight(HttpServletRequest request){
        String rebarDiameter = request.getParameter("rebarDiameter");
        return steelplantPlanService.getRebarWeight(rebarDiameter);
    }
}
